<template>
  <el-card shadow="always">
<!--    用表格显示数据-->
    <el-table
            :data="students"
            style="width: 100%">
      <el-table-column
              label="学号"
              prop="sid"
              align="center"
              width="180"/>
      <el-table-column
              label="姓名"
              prop="sname"
              align="center"/>
      <el-table-column
              label="性别"
              prop="sex"
              align="center"/>
      <el-table-column
              label="年龄"
              prop="age"
              align="center"/>
      <el-table-column
              label="住址"
              prop="addr"
              align="center"/>
      <el-table-column
              label="所在班级"
              prop="cid"
              align="center"/>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
                  size="mini"
                  @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
<!--    3. 绘制分页导航-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
  </el-card>

</template>

<script>
  export default {
    data() {
      return {
        students: [],
        page: 1,      // 当前页
        pageSize: 5,  // 每页大小
        total: 100,   // 总记录数
      }
    },
    created() {
      // this.findAllStudents()     // 查询所有学生
      this.findByPage()             // 普通分页查询
    },
    methods: {
      //1. 查询所有学生
      findAllStudents(){
        this.$axios.get("/student/list.do").then(({data}) => {
          this.students = data.data
        })
      },
      //2. 分页查询
      findByPage(){
        this.$axios.get(`/student/findByPage.do?page=${this.page}&pageSize=${this.pageSize}`).then(({data}) => {
          console.log("data:",data)
          this.students = data.data.rows      //  data.data: 相当于后台的PageResult对象
          this.total = data.data.total
        })
      },
      // pageSize页大小改变时触发
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val
        this.findByPage()
      },
      // page当前页改变时触发
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.page = val
        this.findByPage()
      },
      handleEdit(index, row) {
        console.log(index, row);
      },
      handleDelete(index, row) {
        console.log(index, row);
      }
    }
  }
</script>

<style>
  .el-pagination{
    margin-top: 20px;
    text-align: right;
  }
</style>